﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evpo.ContactData.ViewModel;
using Evpo.ContactData.Common;

namespace Evpo.ContactData.UI
{
    public partial class ContactSummary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewModel = new ContactSummaryViewModel(
                new ModelFactory(),
                (IConnection)Session[SessionStateKeys.Connection],
                Int32.Parse(Request["ID"])
            );

            if (!IsPostBack)
            {
                uiLinkedGroup.DataSource = ViewModel.ContactLinks(ViewModel.ContactID);
                DataBind();
            }
        }

        #region Fields
        #endregion

        #region Properties
        protected Uri EditAddressUrl
        {
            get
            {
                if(!AddressID.HasValue)
                    return null;

                return UrlHelper.AddressUrl(
                    AddressID.Value,
                    Request.Url,
                    ViewModel.ContactID);
            }
        }

        protected ContactSummaryViewModel ViewModel
        {
            get;
            set;
        }

        protected bool IsAddressVisible
        {
            get
            {
                return uiContactEdit.Contact.AddressID.HasValue;
            }
        }

        protected int? AddressID
        {
            get
            {
                return uiContactEdit.Contact.AddressID;
            }
        }
        #endregion

        #region Methods
        public string AddressSelectorUrl()
        {
            return UrlHelper.ContactUrl(
                ViewModel.ContactID, Request.Url, typeof(AddressSelector).Name).ToString();
        }

        public string ContactEditFormUrl()
        {
            return UrlHelper.ContactUrl(
                ViewModel.ContactID, Request.Url, typeof(ContactEditForm).Name).ToString();
        }

        public string LinkedGroupEditFromUrl()
        {
            return UrlHelper.ContactUrl(
                ViewModel.ContactID, Request.Url, typeof(LinkedGroupEdit).Name).ToString();
        }
        #endregion
    }
}